package com.stylefeng.guns.modular.system.controller;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.SqlRunner;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.stylefeng.guns.common.persistence.model.TimerLog;
import com.stylefeng.guns.core.util.ToolUtil;
import com.stylefeng.guns.modular.system.service.ITimerLogService;
import com.stylefeng.guns.core.base.controller.BaseController;
import com.stylefeng.guns.modular.system.warpper.TimerLogWarpper;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.beans.factory.annotation.Autowired;
import com.stylefeng.guns.core.log.LogObjectHolder;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.List;
import java.util.Map;

/**
 * 控制器
 *
 * @author fengshuonan
 * @Date 2018-01-24 14:13:02
 */
@Controller
@RequestMapping("/timerlog")
public class TimerLogController extends BaseController {

    private String PREFIX = "/system/timerLog/";

    @Autowired
    private ITimerLogService timerLogService;

    /**
     * 跳转到首页
     */
    @RequestMapping("")
    public String index() {
        return PREFIX + "timerLog.html";
    }

    /**
     * 跳转到添加
     */
    @RequestMapping("/timerLog_add")
    public String timerLogAdd() {
        return PREFIX + "timerLog_add.html";
    }

    /**
     * 跳转到修改
     */
    @RequestMapping("/timerLog_update/{timerLogId}")
    public String timerLogUpdate(@PathVariable Integer timerLogId, Model model) {
        TimerLog timerLog = timerLogService.selectById(timerLogId);
        model.addAttribute("item",timerLog);
        LogObjectHolder.me().set(timerLog);
        return PREFIX + "timerLog_edit.html";
    }

    /**
     * 获取列表
     */
    @RequestMapping(value = "/list")
    @ResponseBody
    public Object list(String condition) {
        if(ToolUtil.isNotEmpty(condition)){
            EntityWrapper<TimerLog> entityWrapper = new  EntityWrapper<>();
          Wrapper<TimerLog> wrapper  = entityWrapper.like("timer_name","%"+condition+"%");
            List<Map<String ,Object>> timerlog= timerLogService.selectMaps(wrapper);
            return super.warpObject(new TimerLogWarpper(timerlog));
        }else{
            List<Map<String ,Object>> log= timerLogService.selectMaps(null);
            return super.warpObject(new TimerLogWarpper(log));
        }
    }

    /**
     * 新增
     */
    @RequestMapping(value = "/add")
    @ResponseBody
    public Object add(TimerLog timerLog) {
        timerLogService.insert(timerLog);
        return super.SUCCESS_TIP;
    }

    /**
     * 删除
     */
    @RequestMapping(value = "/delete")
    @ResponseBody
    public Object delete(@RequestParam Integer timerLogId) {
        timerLogService.deleteById(timerLogId);
        return SUCCESS_TIP;
    }

    /**
     * 修改
     */
    @RequestMapping(value = "/update")
    @ResponseBody
    public Object update(TimerLog timerLog) {
        timerLogService.updateById(timerLog);
        return super.SUCCESS_TIP;
    }

    /**
     * 详情
     */
    @RequestMapping(value = "/detail/{timerLogId}")
    @ResponseBody
    public Object detail(@PathVariable("timerLogId") Integer timerLogId) {
        return timerLogService.selectById(timerLogId);
    }

    /**
     * 清空所有日志
     */
    @RequestMapping(value = "/deletelog")
    @ResponseBody
    public Object deletelog() {
        SqlRunner.db().delete("delete from sys_timer_log");
        return super.SUCCESS_TIP;
    }
}
